#include "../server.hpp"

class EchoServer{
private:
    TcpServer _tcp_server;
private:
    void OnConnected(const PtrConnection &conn){
        DEBUG_LOG("New Connection: %p", conn.get());
    }
    void OnClosed(const PtrConnection &conn)
    {
        DEBUG_LOG("Connection: %p Closed", conn.get());
    }
    void OnMessage(const PtrConnection &conn, Buffer *buf){
        DEBUG_LOG("%s", buf->read_pos());
        conn->Send(buf->read_pos(), buf->readable_size());
        buf->move_reader(buf->readable_size());
        conn->ShutDown();
    }
public:
    EchoServer(int port):_tcp_server(port){
        _tcp_server.SetThreadCount(5);
        _tcp_server.EnableInactiveRelease(10);
        _tcp_server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _tcp_server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _tcp_server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }    
    void Start(){
        _tcp_server.Start();
    }
};